package com.linb.pan.server.modules.recycle.controller;

import com.google.common.base.Splitter;
import com.linb.pan.core.constants.BPanConstants;
import com.linb.pan.core.response.R;
import com.linb.pan.core.utils.IdUtil;
import com.linb.pan.server.common.utils.UserIdUtil;
import com.linb.pan.server.modules.file.vo.BPanUserFileVO;
import com.linb.pan.server.modules.recycle.context.DeleteContext;
import com.linb.pan.server.modules.recycle.context.QueryRecycleFileListContext;
import com.linb.pan.server.modules.recycle.context.RestoreContext;
import com.linb.pan.server.modules.recycle.po.DeletePO;
import com.linb.pan.server.modules.recycle.po.RestorePO;
import com.linb.pan.server.modules.recycle.service.IRecycleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "回收站模块")
@RestController
@Validated
public class RecycleController {

    @Autowired
    private IRecycleService recycleService;

    @ApiOperation(value = "查询回收站文件列表")
    @GetMapping("recycles")
    public R<List<BPanUserFileVO>> recycles() {
        QueryRecycleFileListContext context = new QueryRecycleFileListContext();
        context.setUserId(UserIdUtil.get());
        List<BPanUserFileVO> fileList = recycleService.recycles(context);
        return R.data(fileList);
    }

    @ApiOperation(value = "批量还原文件")
    @PutMapping("recycle/restore")
    public R restore(@Validated @RequestBody RestorePO restorePO) {
        RestoreContext restoreContext = new RestoreContext();
        restoreContext.setUserId(UserIdUtil.get());
        //String转换为List<Long>
        List<Long> fileIdList = Splitter.on(BPanConstants.COMMON_SEPARATOR).splitToList(restorePO.getFileIds()).stream()
                .map(IdUtil::decrypt)
                .collect(Collectors.toList());
        restoreContext.setFileIdList(fileIdList);
        recycleService.restore(restoreContext);
        return R.success();
    }

    @ApiOperation(value = "彻底删除文件")
    @DeleteMapping("recycle")
    public R delete(@Validated @RequestBody DeletePO deletePO) {
        DeleteContext context = new DeleteContext();
        List<Long> fileIdList = Splitter.on(BPanConstants.COMMON_SEPARATOR).splitToList(deletePO.getFileIds()).stream().map(IdUtil::decrypt).collect(Collectors.toList());
        context.setUserId(UserIdUtil.get());
        context.setFileIdList(fileIdList);
        recycleService.delete(context);
        return R.success();
    }

}
